package com.gxa.modules.sys.controller;

import com.gxa.common.utils.Result;
import com.gxa.modules.sys.dto.MyOrderSetDto;
import com.gxa.modules.sys.dto.OederSeatDto;
import com.gxa.modules.sys.dto.TimeDto;
import com.gxa.modules.sys.dto.UpdateSeatDto;
import com.gxa.modules.sys.service.OrderSeatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(tags = "预约座位管理接口")
@RestController
public class OrderSeatController {


    @Autowired
    private OrderSeatService orderSeatService;

    @ApiOperation(value = "点击预约座位，获取预约座位数据")
    @PostMapping("/orderseat/queryall")
    public
    Result queryAll(@RequestBody TimeDto timeDto) {
        try {

            List<OederSeatDto> oederSeatDtos = orderSeatService.queryAll(timeDto);

            return new Result().ok(oederSeatDtos);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("查询数据失败");


        }
    }


    @ApiOperation(value = "点击预约支付已完成，创建订单")
    @PutMapping("/orderseat/creteOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "createTime", value = "查询条件：开始时间", dataType = "Date"),
    })
    public
    Result updateStatus(@RequestBody MyOrderSetDto myOrderSetDto) {
        try {

            String purchaseNo = createPurchaseNo();
            myOrderSetDto.setNo(purchaseNo);
            Date date = new Date(System.currentTimeMillis());

            long time = date.getTime();
            java.sql.Date date1 = new java.sql.Date(time);

            myOrderSetDto.setCreateTime(date1);
            orderSeatService.updateState(myOrderSetDto);


            return new Result().ok();

        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("修改数据失败");


        }

    }


    @ApiOperation(value = "点击改签")
    @PutMapping("/orderseat/updateSeat")
    public
    Result updateSeat(@RequestBody UpdateSeatDto updateSeatDto) {
        try {

            orderSeatService.updateSeat(updateSeatDto);

            return new Result().ok("改签成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("改签失败");


        }
    }


    @ApiOperation(value = "查询月，日")
    @GetMapping("/orderseat/queryYd")
    public
    Result queryMd() {
        try {

            List<TimeDto> timeDtos = orderSeatService.queryMd();

            return new Result().ok(timeDtos);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("查询失败");


        }
    }


    @ApiOperation(value = "取消订单")
    @PutMapping("/orderseat/updateCancel")
    public
    Result updateCancel( Integer myorderId) {
        try {

            orderSeatService.updateCancel(myorderId);
            return new Result().error("2");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result().error("取消失败");


        }
    }



















//    public static void main(String[] args) {
//        String purchaseNo = createPurchaseNo();
//        System.out.println(purchaseNo);
//    }

    //构建定单编号
    private static String createPurchaseNo(){
        StringBuilder sb = new StringBuilder();

        //获取当前日期
        Date date = new Date(System.currentTimeMillis());
        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDay();
        sb.append("dd" + year + month + day);

        Random random = new Random();
        int ends = random.nextInt(99);

        String format = String.format("%02d", ends);//如果不足两位，前面补0
        sb.append(format);

        return sb.toString();
    }



}
